<?php
namespace App\Models\V1;
use Illuminate\Database\Eloquent\Model;
use DB;
class Active666Model extends Model
{
    public function get_activity_info($activityId, $openid = '')
    {
        if ($activityId > 0) {
            $info = DB::table('666_activity')->where('activity_id',$activityId)->where('delete_at')->first();
        } else if ($openid != '') {
            $info = DB::table('666_activity')->where('openids', 'like', "%" . $openid . "%")->where('delete_at')->first();
        } else {
            $info = null;
        }

        if (!$info) {
            return null;
        }

        $info->ad_imgs = json_decode($info->ad_imgs,true);
        $info->detail_info  = json_decode($info->detail_info,true);

        $time               = strtotime($info->end_time) - time();
        $day                = $time > 0 ? $time / (3600*24) : 0;
        $day                = (int) $day;
        $info->beginTime    = str_replace('-',"/",substr($info->begin_time,0,11));
        $info->surplusDay   = $day;
        return $info;
    }
    /**
     * 获取订单信息
     * @param integer $orderId 订单ID
     * */
    public function get_order_info($orderId)
    {
        $orderInfo  = DB::table('666_order')
            ->where('id',$orderId)
            ->first();
        return $orderInfo;
    }

    /**
     * 更改订单信息
     * */
    public function update_order_info($orderId,$info)
    {
        DB::table('666_order')->where('id',$orderId)->update($info);
    }

    /**
     * 添加支付日志
     * */
    public function add_pay_log($data)
    {
        return DB::table('666_order_pay_log')->insertGetId($data);
    }


    /**
     * 获得订单的筹集者
     * */
    public function get_order_supporter($orderId)
    {
        $supports = DB::table('666_order_pay_log')
            ->where('order_id',$orderId)
            ->where('pay_status',1)
            ->orderBy('id',"desc")
            ->get();
        return $supports;

    }


    /**
     * 获得订单列表
     * @param int $managerId 管理员ID
     * @param int $isSuccess 是否完成
     * */
    public function get_orders($managerId,$isSuccess = -1)
    {
        $db     = DB::table('666_order as a')
            ->leftJoin('666_manager as b','a.manager_id','=','b.id')
            ->select('a.*','b.real_name as manager_name');
        if($managerId > 0)
        {
            $db->where('a.manager_id',$managerId);
        }
        if($isSuccess == 0)
        {
            $db->whereNull('a.success_time');
        }
        elseif($isSuccess == 1)
        {
            $db->whereNotNull('a.success_time');
        }

        $orders = $db->whereNull('a.deleted_at')
            ->orderBy('a.success_time')
            ->paginate(10);
        return $orders;
    }


    public function get_user_order($openId,$activityId=0)
    {

        $db = DB::table('666_order')->where('open_id',$openId)->where('deleted_at');
        if($activityId > 0)
        {
            $db->where('activity_id',$activityId);
        }
        return $db->first();
    }

    /**
     * 统计订单
     * @param integer $managerId 管理员ID
     * @param integer $isSuccess 是否成功
     * */
    public function count_order($managerId,$isSuccess = -1){

        $db     = DB::table('666_order');
        if($managerId > 0)
        {
            $db->where('manager_id',$managerId);
        }

        if($isSuccess == 0)
        {
            $db->whereNull('success_time');
        }
        elseif($isSuccess == 1)
        {
            $db->whereNotNull('success_time');
        }
        $orders = $db->whereNull('deleted_at')->count();
        return $orders;
    }
    /**
     * 获取管理员手机
     * @param integer $managerId 管理员ID
     * */
    public function get_manager_info($managerId){

        return DB::table('666_manager')->where('id',$managerId)->first();

    }

    //获取最后一条信息
    public function get_last_manager()
    {
        return DB::table('666_manager')->whereNull('deleted_at')->first();
    }


    /**
     * 删除管理员
     * @param int $managerId 管理员ID
     * */
    public function delete_manager($managerId)
    {
        $this->update_manager($managerId,['deleted_at'=>date_time()]);
    }

    /**
     * 更加手机号获取管理员
     * @param string $mobile 手机号
     * */
    public function get_manager_info_by_mobile($mobile)
    {
        return DB::table('666_manager')->where('mobile',$mobile)->first();
    }


    /**
     * 根据微信ID获取管理员
     * @param string $openId 微信ID
     * */
    public function get_manager_info_by_openid($openId){
        return DB::table('666_manager')->where('open_id',$openId)->first();
    }


    /**
     * 修改管理员信息
     * @param integer $managerId 管理员ID
     * @param array $info 修改的信息
     * */
    public function update_manager($managerId,array $info){

        $info['updated_at'] = date_time();
        DB::table('666_manager')->where('id',$managerId)->update($info);
    }

    /**
     * 添加管理员
     * param array $info 管理员信息
     * */
    public function add_manager(array $info)
    {
        $date   = date_time();
        $info['updated_at'] = $date;
        $info['created_at'] = $date;
        $id = DB::table('666_manager')->insertGetId($info);
        return $id;
    }

    /**
     * 获得所有管理员
     * */
    public function get_managers()
    {
        $managers   = DB::table('666_manager')
            ->whereNull('deleted_at')
            ->paginate(15);
        return $managers;
    }

    /**
     * 根据类型获得管理员
     * @param int $type 管理员类型
     * */
    public function get_managers_by_type($type)
    {
        $managers   = DB::table('666_manager')
            ->whereNull('deleted_at')
            ->where('manager_type',$type)
            ->get();
        return $managers;
    }

}
